from datetime import date, timedelta
from django.contrib.auth.decorators import login_required
from django.shortcuts import render_to_response
from django.template import RequestContext
from lotto.statistic.forms import *
from lotto.statistic.algorithm import *


@login_required ()
def maps (request):
    value = []
    if request.method == 'POST':
        m = request.POST.get ('mode', 'lotto');
        form = MapForm (m, request.POST);
        if form.is_valid ():
            d = form.cleaned_data;
            value = mapsg (d['mode'], d['month'], d['year']);
    else:
        m = request.session.get ('mode', 'lotto');
        d = date.today ();
        form = MapForm (m, initial = {'mode':m, 'month':d.month, 'year':d.year});
        value = mapsg (m, d.month, d.year);
    size = rng (m);
    return render_to_response ('statistic/map.html',
							   {'value':value, 'size':size, 'form':form},
    						   context_instance = RequestContext (request));


def random (request):
    if request.method == 'POST':
        form = ModeForm (request.POST);
        if form.is_valid ():
            value = randomize (request.POST.get ('mode', 'lotto'));
    else:
        m = request.session.get ('mode', 'lotto');
        form = ModeForm (initial = {'mode':m});
        value = randomize (m);
    value.sort ();
    return render_to_response ('statistic/random.html',
							   {'value':value, 'form':form},
    						   context_instance = RequestContext (request));

@login_required ()
def generator (request):
    if request.method == 'POST':
        m = request.POST.get ('mode', 'lotto');
        form = ModeForm (request.POST);
    else:
        m = request.session.get ('mode', 'lotto');
        form = ModeForm (initial = {'mode':m});
    value = [];
    breaker = 10;
    while not value or not breaker:
        value = generate (m);
        breaker -= 1;
    return render_to_response ('statistic/generator.html',
							   {'value':value, 'form':form},
    						   context_instance = RequestContext (request));


def checker (request):
    value = [];
    mode = request.POST.get ('mode', request.session.get ('mode', 'lotto'));
    if request.method == 'POST':
        form = CheckForm (SIZE[mode][1]+1, request.POST);
        if form.is_valid ():
            d = form.cleaned_data;
            value = check (mode, d['date'], int(d['quantity']), list(int(i) for i in d['nums']));
    else:
        form = CheckForm (SIZE[mode][1]+1, initial = {'mode':mode});        
    return render_to_response ('statistic/check.html',
							   {'value':value, 'form':form},
    						   context_instance = RequestContext (request));


def distribution (request):
    value = [];
    if request.method == 'POST':
        form = LottoForm (request.POST);
        if form.is_valid ():
            d = form.cleaned_data;
            value = distrib (d['mode'], d['date']);
    else:
        m = request.session.get ('mode', 'lotto');
        d = date.today () - timedelta (days = 30);
        form = LottoForm (initial = {'mode':m, 'date':d});
        value = distrib (m, d);
    return render_to_response ('statistic/distribution.html',
							   {'value':value, 'form':form},
    						   context_instance = RequestContext (request));


def dist_ten (request):
    value = [];
    if request.method == 'POST':
        form = LottoForm (request.POST);
        if form.is_valid ():
            d = form.cleaned_data;
            value = distribten (d['mode'], d['date']);
    else:
        m = request.session.get ('mode', 'lotto');
        d = date.today () - timedelta (days = 30);
        form = LottoForm (initial = {'mode':m, 'date':d});
        value = distribten (m, d);
    return render_to_response ('statistic/distributionten.html',
							   {'value':value, 'form':form},
    						   context_instance = RequestContext (request));


def dist_unity (request):
    value = [];
    if request.method == 'POST':
        form = LottoForm (request.POST);
        if form.is_valid ():
            d = form.cleaned_data;
            value = distribuni (d['mode'], d['date']);
    else:
        m = request.session.get ('mode', 'lotto');
        d = date.today () - timedelta (days = 30);
        form = LottoForm (initial = {'mode':m, 'date':d});
        value = distribuni (m, d);
    return render_to_response ('statistic/distributionunity.html',
							   {'value':value, 'form':form},
    						   context_instance = RequestContext (request));


def dist_pair (request):
    if request.method == 'POST':
        form = PairForm (request.POST);
        if form.is_valid ():
            d = form.cleaned_data;
            value = distpai (d['mode'], d['date'], int(d['num']));
    else:
        m = request.session.get ('mode', 'lotto');
        d = date.today () - timedelta (days = 30);
        form = PairForm (initial = {'mode':m, 'date':d, 'num':1});
        value = distpai (m, d, 1);
    return render_to_response ('statistic/dist-pair.html',
							   {'value':value, 'form':form},
    						   context_instance = RequestContext (request));


def parity (request):
    value = [0, 0];
    if request.method == 'POST':
        form = LottoForm (request.POST);
        if form.is_valid ():
            d = form.cleaned_data;
            value = pair (d['mode'], d['date']);
    else:
        m = request.session.get ('mode', 'lotto');
        d = date.today () - timedelta (days = 30);
        form = LottoForm (initial = {'mode':m, 'date':d});
        value = pair (m, d);
    return render_to_response ('statistic/parity.html',
							   {'value':value, 'form':form},
    						   context_instance = RequestContext (request));


def relation (request):
    mode = request.session.get ('mode', 'lotto');
    value = {};
    if request.method == 'POST':
        form = LottoForm (request.POST);
        if form.is_valid ():
            d = form.cleaned_data;
            value = relationship (d['mode'], d['date']);
    else:
        m = request.session.get ('mode', 'lotto');
        d = date.today () - timedelta (days = 30);
        form = LottoForm (initial = {'mode':m, 'date':d});
        value = relationship (m, d);
    return render_to_response ('statistic/relation.html',
							   {'value':value, 'form':form},
    						   context_instance = RequestContext (request));


def total (request):
    value = [];
    if request.method == 'POST':
        form = LottoForm (request.POST);
        if form.is_valid ():
            d = form.cleaned_data;
            value = totalsum (d['mode'], d['date']);
    else:
        m = request.session.get ('mode', 'lotto');
        d = date.today () - timedelta (days = 30);
        form = LottoForm (initial = {'mode':m, 'date':d});
        value = totalsum (m, d);
    return render_to_response ('statistic/total.html',
							   {'value':value, 'form':form},
    						   context_instance = RequestContext (request));

@login_required ()
def interval (request):
    value = [];
    if request.method == 'POST':
        form = LottoForm (request.POST);
        if form.is_valid ():
            d = form.cleaned_data;
            value = intervals (d['mode'], d['date']);
    else:
        m = request.session.get ('mode', 'lotto');
        d = date.today () - timedelta (days = 30);
        form = LottoForm (initial = {'mode':m, 'date':d});
        value = intervals (m, d);
    return render_to_response ('statistic/interval.html',
							   {'value':value, 'form':form},
    						   context_instance = RequestContext (request));

@login_required ()
def int_pair (request):
    if request.method == 'POST':
        form = PairForm (request.POST);
        if form.is_valid ():
            d = form.cleaned_data;
            value = intpai (d['mode'], d['date'], int(d['num']));
    else:
        m = request.session.get ('mode', 'lotto');
        d = date.today () - timedelta (days = 30);
        form = PairForm (initial = {'mode':m, 'date':d, 'num':1});
        value = intpai (m, d, 1);
    return render_to_response ('statistic/int-pair.html',
							   {'value':value, 'form':form},
    						   context_instance = RequestContext (request));
